﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NUS.ISS.ARS.Web.PageController;
using NUS.ISS.ARS.BusinessEntity.ViewModels;

namespace NUS.ISS.ARS.Web.Booking
{
    public partial class MyBookings : BookingBaseController
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CurrentPage = "MyBookings.aspx";
            base.Page_Load(sender, e);

            if (!Page.IsPostBack)
            {
                lblMsg.Text = "";
                BindData();
            }
        }

        private void BindData()
        {
            if (FlightBookingTrips.Count() > 0)
            {
                lvBookings.DataSource = FormatFlightBookingTripViewForDisplay();
                lvBookings.DataBind();
            }
            else
            {
                lblMsg.Text = "No bookings found.";
            }
        }
        
        protected List<object> FormatFlightBookingTripViewForDisplay()
        {
            List<object> trips = new List<object>();           
            string mformat = "$ {0}";
            foreach (FlightBookingTripView t in FlightBookingTrips)
            {
                trips.Add(new
                {
                    id = t.FlightBookingTripId,                    
                    titleFromTo = String.Format("{0} to {1}", t.FromCity, t.ToCity),
                    total_price = String.Format(mformat, + t.TotalPrice),
                    from_to_time = String.Format("{0} ({1}) - {2} ({3})", t.DepartureTime.ToString(@"hh\:mm"), t.DepartureCityCode, t.ArrivalTime.ToString(@"hh\:mm"), t.ArrivalCityCode),
                    long_date = t.FlightDate.ToLongDateString(),
                    flight_no = "Flight: " + t.FlightNo,                   
                    fare = "Fare (" + t.BookingTripDetailViews.Count() + " pax): " + String.Format(mformat, t.TotalPrice) + " + ",                    
                    surcharge = "Surcharge: " + String.Format(mformat, Convert.ToDecimal(t.Surcharge).ToString("#0.00"))
                });
            }

            return trips;
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            List<long> tripIds = new List<long>();

            foreach (ListViewItem item in lvBookings.Items)
            {
                var rdo = (CheckBox) item.FindControl("chkChange");
                long tripId = 0;

                if (rdo.Checked)
                {
                    long.TryParse(((Label)item.FindControl("lblId")).Text, out tripId);
                }

                if (tripId != 0) tripIds.Add(tripId);                
            }

            MarkToChangeBookingTrip(tripIds);

            MoveToNextPage();
        }

       
    }
}